﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Subdivision : IEquatable<Subdivision>
    {
        public void ImportList(List<Classes.Subdivision> subdivisionList)
        {
            var mdb = new MedregDBDataContext();
            var ListSubdivision = new List<Subdivision>();
            foreach (var subdivision in subdivisionList)
            {
                Subdivision subdivision1 = new Subdivision() { ID = subdivision.ID, Name = subdivision.Name };
                Subdivision query;

                if (mdb.Subdivision.LongCount() == 0)
                {
                    mdb.Subdivision.InsertOnSubmit(subdivision1);
                }
                else
                {
                    try
                    {
                        query = mdb.Subdivision.Single(l => l.ID == subdivision1.ID);
                        query.Name = subdivision1.Name;
                        
                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {
                        if (!ListSubdivision.Contains(subdivision1))
                        {
                            mdb.Subdivision.InsertOnSubmit(subdivision1);
                            ListSubdivision.Add(subdivision1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Subdivision other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
